/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TheBoard;

import java.awt.Component;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



/**
 *
 * @author Windows7
 */
public class BackGround extends JPanel{
    
    BufferedImage img;
    
    private int x;
    private int y;
    
    private int height;
    private int width;
    

    @Override
    public void paint(Graphics g)
    {
        //this.paint(g);
        //Rectangle rectangleAreaToDrawImage = new Rectangle(this.x, this.y, this.width, this.height);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(img, 0, 0,this);
        //g.dispose();
    }
    
    public BackGround(String File)
    {
        try
        {
            img = ImageIO.read(new File(File));
        } 
        catch(IOException e)
        {
            
        }
    }   
    
    public BackGround(String File, int x, int y)
    {
        this.x = x;
        this.y = y;
        
        try
        {
            img = ImageIO.read(new File(File));
            this.height = img.getHeight();
            this.width = img.getWidth();  
            this.setBounds(this.x, this.y, this.width, this.height);
        } 
        catch(IOException e)
        {
            System.out.printf("Damn it! you fail to load "+ File);
        }
    }  
       
    @Override
    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(400,400);
        } else {
           return new Dimension(this.width + this.x, this.height +  this.y);
       }
    }
    

    
    public static void main(String[] args)
    {
        JFrame j = new JFrame();
        j.addWindowListener(new WindowAdapter(){
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        j.add(new BackGround("ChessForImages/Middle Ages Theme/mainBoard.png",25,25));
        //j.pack();
        j.setVisible(true);
    }

    /**
     * @return the x
     */
    @Override
    public int getX() {
        return this.x;
    }

    /**
     * @return the y
     */
    @Override
    public int getY() {
        return this.y;
    }

    /**
     * @return the height
     */
    @Override
    public int getHeight() {
        return this.height;
    }

    /**
     * @return the width
     */
    @Override
    public int getWidth() {
        return this.width;
    }
}
